import {observable, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {addCamera} from '@/api/ezviz'
import Taro from '@tarojs/taro'
import {getStorage} from '@/utils/localstoreage'
class Addvideosurveillance extends ChildrenPageUtils {
  @observable cameraData: Camera.Info = {
    cameraId: -1,
    cameraName: '',
    cameraUrl: '',
    appKey: '',
    appSecret: '',
    accessToken: '',
    userId: -1,
    deviceSerial: '',
    channelNo: ''
  }

  @action.bound
  setCameraDta(data: Camera.Info) {
    this.cameraData = data
  }
  @action.bound
  clearCameraDta() {
    this.cameraData = {
      cameraId: -1,
      cameraName: '',
      cameraUrl: '',
      appKey: '',
      appSecret: '',
      accessToken: '',
      userId: -1,
      deviceSerial: '',
      channelNo: ''
    }
  }
  @action.bound
  initCameraDta() {
    this.cameraData = {
      cameraId: -1,
      cameraName: '',
      cameraUrl: '',
      appKey: '',
      appSecret: '',
      accessToken: '',
      userId: -1,
      deviceSerial: '',
      channelNo: ''
    }
  }
  @action.bound
  handleInputChange(key: keyof Camera.Info, data: string) {
    if (this.cameraData !== undefined) {
      this.cameraData[key] = data as never
    }
  }

  @action.bound
  validateDataSourceBySubmit(): boolean {
    const {cameraName, cameraUrl, appKey, appSecret, deviceSerial, channelNo} =
      this.cameraData

    if (!cameraName) {
      Taro.showModal({
        content: '摄像头名称未输入'
      })
      return false
    }
    if (!appKey) {
      Taro.showModal({
        content: 'AppKey未输入'
      })
      return false
    }
    if (!appSecret) {
      Taro.showModal({
        content: 'AppSecret未输入'
      })
      return false
    }
    if (!deviceSerial) {
      Taro.showModal({
        content: '设备序列号未输入'
      })
      return false
    }
    if (!channelNo) {
      Taro.showModal({
        content: '通道号未输入'
      })
      return false
    }
    if (!cameraUrl) {
      Taro.showModal({
        content: '监控预览地址未输入'
      })
      return false
    }
    return true
  }
  @action.bound
  handleSubmit() {
    const userId = getStorage('userId')
    if (userId) {
      console.log(this.cameraData)
      const {
        cameraName,
        cameraUrl,
        appKey,
        appSecret,
        deviceSerial,
        channelNo
      } = this.cameraData
      // 校验参数
      if (!this.validateDataSourceBySubmit()) return
      addCamera({
        userId,
        cameraName,
        cameraUrl,
        appKey,
        appSecret,
        deviceSerial,
        channelNo
      })
        .then(res => {
          if (res.msg) {
            Taro.showModal({
              content: res.msg
            })
          }
          this.initCameraDta()
        })
        .catch(err => {
          if (err) {
            Taro.showModal({
              content: err.msg
            })
          }
        })
    }
  }
}

export const addVideoSurveillanceStore = createContext(
  new Addvideosurveillance()
)
